/*
 * Test of noise gradient functions for 2, 3 and 4 dimensions.
 * The 3D version is Ken Perlin's original code.
 * The 2D and 4D versions are my own, in the same ugly
 * style which is more suited for C or C++ than Java.
 * (Java is not very fast with bit-wise operations.)
 *
 * (TODO: find a good gradient function also for the 1D case.)
 *
 * Author: Stefan Gustavson (stegu@itn.liu.se) 2005-11-06
 */

public class ImprovedNoiseGradTest {

  private static double grad(int hash, double x, double y) {
    int h = hash & 7;                      // Convert low 3 bits of hash code
    double u = h<4 ? x : y;                 // into 8 gradient directions.
    double v = h<4 ? y : x;
    return ((h&1) == 0 ? u : -u) + ((h&2) == 0 ? 2.0*v : -2.0*v);
  }
  
  private static double grad(int hash, double x, double y, double z) {
    int h = hash & 15;                      // Convert low 4 bits of hash code
    double u = h<8 ? x : y;                 // into 12 gradient directions.
    double v = h<4 ? y : h==12||h==14 ? x : z;
    return ((h&1) == 0 ? u : -u) + ((h&2) == 0 ? v : -v);
  }
  
  private static double grad(int hash, double x, double y, double z, double t) {
    int h = hash & 31;                      // Convert low 5 bits of hash code
    double u = h<24 ? x : y;                 // into 32 gradient directions.
    double v = h<16 ? y : z;
    double w = h<8 ? z : t;
    return ((h&1)==0 ? u : -u) + ((h&2) == 0 ? v : -v) + ((h&4) == 0 ? w : -w);
  }
  
  public static void main(String args[]) {
	double x = 1;
	double y = 10;
	double z = 100;
	double t = 1000;
	System.out.println("(x, y) = "+x+", "+y);
	for(int i =0; i<8; i++) {
		System.out.println("Hash "+i+": gradient "+grad(i,x,y));
	}
	System.out.println("(x, y, z) = "+x+", "+y+", "+z);
	for(int i =0; i<16; i++) {
		System.out.println("Hash "+i+": gradient "+grad(i,x,y,z));
	}
	System.out.println("\n(x, y, z, t) = "+x+", "+y+", "+z+", "+t);
	for(int i =0; i<32; i++) {
		System.out.println("Hash "+i+": gradient "+grad(i,x,y,z,t));
	}
  }

}
